package com.example.note2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 居伦昶 on 2018/6/21.
 */

public class AddContent extends Activity {
    private Button savebtn,deletebtn;
    private EditText eetext;
    private NotesDB notedb;//创建数据库对象
    private SQLiteDatabase dbWriter;//获取权限对象
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcontent);
        savebtn=findViewById(R.id.save);
        deletebtn=findViewById(R.id.delete);
        eetext= findViewById(R.id.ettext);
        notedb=new NotesDB(this);//实例化数据库对象
        dbWriter=notedb.getWritableDatabase();//获取权限
        savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addDB();
                finish();
            }
        });
        deletebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    public void addDB(){//添加数据方法
        ContentValues cv=new ContentValues();
        cv.put(NotesDB.CONTENE,eetext.getText().toString());
        cv.put(NotesDB.TIME,getTime());
        dbWriter.insert(NotesDB.TABLE_NAME,null,cv);
    }
    public String getTime(){
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format =new SimpleDateFormat("yyyy年MM月dd日 HH：mm：ss");
        Date date =new Date();
        String str=format.format(date);
        return str;

    }
}
